package org.vgou.service.impl;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.vgou.service.IEmailService;
import org.vgou.util.EmailTools;

/**
 * 
 * Copyright (c) 2011, vgou <br>
 * All rights reserved.<br>
 * 
 * @since 2010-11-27
 * @author <a href="mailto:jason.hxx@gmail.com">jason</a>
 * @version Id: EmailServiceImpl.java 
 * @comment	邮件服务类
 * 
 */
@Service("emailService")
public class EmailServiceImpl<T> implements IEmailService {
	protected final Log logger = LogFactory.getLog(getClass());

	@Qualifier("mailSender")
	@Resource
	private JavaMailSender sender;

	@Resource
	TaskExecutor taskExecutor;// 注入Spring封装的异步执行器

	private String mailFrom;

	/**
	 * 异步方式发送邮件
	 * 
	 * @param toUser
	 *            接受用户邮件地址
	 * @param subject
	 *            主题
	 * @param content
	 *            内容
	 * @param html
	 *            是否HTML格式
	 */
	public void sendMailByAsynchronousMode(final String toUser,
			final String subject, final String content, final boolean html) {
		try {
			taskExecutor.execute(new Runnable() {
				
				@Override
				public void run() {
					try {
						EmailTools.getInstance().sendMail(sender, toUser,
								mailFrom, subject, content, html);
					} catch (Exception e) {
						logger.error(e);
					}
				}
			});
		} catch (Exception e) {
			logger.error("发送邮件失败:" + e.getMessage());
		}
	}
	
}
